作为一名 IT,和日志打交道是必不可少的,我们经常需要去查看一些日志文件,以从中获取一些有用的信息,而日志都是以文本形式存在的,今天我们就来看看,使用 C# 如何来处理文本。
我以一个人 Nginx 的日志文件为例(日志是重新自定义后的格式,所以与标准的 Nginx 日志,有可能有区别。)
日志文件内容(部分,IP 信息以及被处理):
192.168.1.98 - - [20/Sep/2021:00:51:13 +0800] "GET / HTTP/1.1" 200 396 "-" "Linux Gnu (cow)"
192.168.1.180 - - [20/Sep/2021:01:29:34 +0800] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36"
192.168.1.26 - - [20/Sep/2021:02:18:58 +0800] "GET / HTTP/1.1" 200 396 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36"
192.168.1.218 - - [20/Sep/2021:02:32:23 +0800] "\x16\x03\x01\x00\xE3\x01\x00\x00\xDF\x03\x03\xB9\xE3\xD5\x0C\xD9b\xD9j9\xCBg\x1B\xD6\x9Ab\xB5\xF8X0\xF9\x03\xB8\x9C\xBF\xECb\xC5\xBAc\x9B\xF4\x91\x00\x00h\xCC\x14\xCC\x13\xC0/\xC0+\xC00\xC0,\xC0\x11\xC0\x07\xC0'\xC0#\xC0\x13\xC0\x09\xC0(\xC0$\xC0\x14\xC0" 400 166 "-" "-"
192.168.1.224 - - [20/Sep/2021:02:32:25 +0800] "GET / HTTP/1.0" 200 612 "-" "-"
192.168.14.209 - - [20/Sep/2021:02:36:40 +0800] "GET /index.php HTTP/1.1" 404 162 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0"
192.168.1.209 - - [20/Sep/2021:02:36:40 +0800] "GET /phpmyadmin/index.php HTTP/1.1" 404 162 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0"
192.168.1.196 - - [20/Sep/2021:02:45:57 +0800] "GET / HTTP/1.0" 200 612 "-" "-"
192.168.1.182 - - [20/Sep/2021:02:54:14 +0800] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
192.168.1.58 - - [20/Sep/2021:03:03:49 +0800] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (compatible; Nimbostratus-Bot/v1.3.2; http://cloudsystemnetworks.com)"
192.168.1.87 - - [20/Sep/2021:03:12:58 +0800] "GET / HTTP/1.0" 200 612 "-" "-"
192.168.1.242 - - [20/Sep/2021:03:38:50 +0800] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/601.7.7 (KHTML, like Gecko) Version/9.1.2 Safari/601.7.7"
192.168.1.232 - - [20/Sep/2021:03:39:50 +0800] "145.ll|'|'|SGFjS2VkX0Q0OTkwNjI3|'|'|WIN-JNAPIER0859|'|'|JNapier|'|'|19-02-01|'|'||'|'|Win 7 Professional SP1 x64|'|'|No|'|'|0.7d|'|'|..|'|'|AA==|'|'|112.inf|'|'|SGFjS2VkDQoxOTIuMTY4LjkyLjIyMjo1NTUyDQpEZXNrdG9wDQpjbGllbnRhLmV4ZQ0KRmFsc2UNCkZhbHNlDQpUcnVlDQpGYWxzZQ==12.act|'|'|AA==" 400 166 "-" "-"
192.168.1.22 - - [20/Sep/2021:03:40:46 +0800] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
下面我们来看一下代码:
using System;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
int counter = 0; // 定义一个计数器,用于计算行
string line;
System.IO.StreamReader file = new //通过 StreamReader 类来读取文件内容 System.IO.StreamReader(@"C:\Users\Admin\Desktop\access.log");
while ((line = file.ReadLine()) != null) //通过循环的方式,每次读取一行
{
// 以空格为分隔符,分割文件,并写入到数组
string[] array = line.Trim().Split(" ");
System.Console.WriteLine(array[0]); //从日志内容,我们可以知道,第一列就是 IP
counter++;
}
file.Close(); // 读取完成后,关闭文件
System.Console.WriteLine($"There were {counter} lines."); // 统计行数
// 用于命令运行完成后,不自动退出,没有编译前,这个功能可以通过 vs 自带的功能实现。
System.Console.ReadLine();
}
}
}
运行结果